var mnc = {
	ctx : '',
	supr : ''
};
function validateForm(form){
	
 	if (form.valid){
		var x = (form).valid();
 		if (!x){
		//	alert("please validate your input");
 			return false;
		}else{
			return true;
		}
		
	} else{
	//	console.dir("no valid");
		return true;
	}
 }


$(function(){
	
	$(document).delegate(".tfck","change",function(){
		if ($(this).is(':checked')) {
			$(this).val(true);
        }else{
			$(this).val(false);
        }
		
	});
	
	
	$('label.required').each(function(){
		var content = $(this).text();
		$(this).html($('<span>').text('*').css('color','red')).append(content);
	});
	$('.btn.btn-danger').not("[type=submit]").click(function(event ){
		event.preventDefault();
		 window.history.back();
		
	});
	
	$('.normal-toggle-button').toggleButtons({
			onChange : function(x,y,z){
				  $($(x).siblings()).each(function(){
					  $(this).prop('checked', false);
					  $(this).removeAttr('checked');
				  });
				var g = $(x).siblings("[value="+y+"]");
				$(g).attr("checked","checked");
			}}
	
	);
	
	
	$("td button.btn-update").click(function(){
		var data = [];
            $(this).parents("tr:first").find('input,select,textarea').each(function(){
    			data = $.merge([ {
					name : $(this).attr('name'),
					value : $(this).val()
				} ], data);
            });
  	var submit = {
        			type : "POST",
        			data : data,
        			dataType : "json",
        			success : function(data) {
        				if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
        					johnny.error(data.message);
        					return;
        				} else {
        					if (data.entity){
        						url = data.entity;
         					}
        					johnny.info("update successfully");
        				}
        			}
        		};
	submit.url = $(this).attr("url");
		$.ajax(submit);
            
});
	
	
	
		$('.simple-submit').click(function(e){
				if ($(this).attr("type")=='submit'){
					e.preventDefault();
					
				}
		 		var form =	$(this).parents("form:first");
		 		var action = form.attr('action');
		 		if (!validateForm(form)){
		 			return;
		 		}
		 													$
															.ajax({
																type : "POST",
																url  : action,
																data : form.serialize(),
																dataType : "json",
																success : function(data) {
																	if (data
																			&& data.state
																			&& (data.state
																					.indexOf("ERROR") != -1)) {
//																		alert(data.message);
																		
																	} else {
																		if (data.message){
																			johnny.error(data.message);
		 																}else{
		 																	var x = form.attr("model-hide");
		 																	if (x){
		 																		
		 																		$(form.attr("href")).modal('hide')	
		 																	}
		 																	johnny.info('Your request has been processed successfully');
		  																}
																	
																	}
																}
															});
		 	 	});
	
	$('.modal-submit').click(function(){
 		var div  = $(this).parents("div.modal:first");
  				$.ajax({
					url : $(div).find("input[name='action']").val(),
					type : "POST",
					data : div.find("select, textarea, input").serialize(),
					dataType : "json",
					success : function(data){		
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
							johnny.error(data.message);
							return;
						}else{
								if (div.attr('reload')){
								 	location.reload();
								}
 								div.modal('hide');
							var x =	div.attr('selector');
								$(x).dataTable().fnDraw();								
 								johnny.info("operate successfully");
							}
					}
				});
 	});
	 
	if (typeof(CKFinder) ==  "function"){
		
		$("input[name='image'],input[name='imageURL'],input.ckfile").click(function(){

	 		var finder = new CKFinder();
	 		finder.basePath = '/ckfinder/';	// The path for the installation of CKFinder (default = "/ckfinder/").
	 		finder.selectActionFunction = function(fileUrl,data ){
	  			 data.selectActionData.val(fileUrl);
	   		};
	 		finder.selectActionData  = $(this);
	 		finder.popup();

	 	 });
 
	}else{
//		console.dir("No..")
	}
	
	
	
});


$(document).ready(function() {
	if ($('.datepicker').length) {
		$(".datepicker").each(function(){
			var pattern = 'yy-mm-dd';
			var this_pattern = $(this).attr('pattern');
			var month = ("true" == $(this).attr('month'))
			var year  =  ("true" == $(this).attr('year'));
				if (this_pattern){
				pattern = this_pattern
			}
			$(this).datepicker({
				dateFormat : pattern,
				showOtherMonths : true,
				changeMonth: month,
				changeYear: year,
				});
				
			
		});
	}
	if ($.fn.datetimepicker){
	if ($('.datetimepicker').length) {
		$(".datetimepicker").each(function(){
			var pattern = 'yy-mm-dd';
			$(this).datetimepicker({
				dateFormat: 'yy-mm-dd', 
				timeFormat: 'hh:mm:ss',
				showSecond: true
				});
				
			
		});
	}}
	
	$("select.auto-country-id").each(function(step,value){
		if (countries){
			var singaporeId;
			$(countries).each(function(s,v){
					
				   $(value).append($('<option>', { 
				        value: v.id,
				        text : v.name
				    }));
				   if (v.name == 'Singapore'){
					   singaporeId = v.id;
				   }
			});
			if (singaporeId){
				$(value).val(singaporeId);							
			}

			
		}
		
	});	
	
	
	
	

});